/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.arthas.psimobile.fragment.report;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.arthas.psimobile.core.BaseFragment;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
import com.xuexiang.psimobile.R;
import com.xuexiang.psimobile.databinding.FragmentReportViewBinding;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import java.util.ArrayList;

@Page(anim = CoreAnim.none, name = "信用评级报告")
public class ReportViewFragment extends BaseFragment<FragmentReportViewBinding> implements View.OnClickListener {

    RadarChart chart;

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle();
        titleBar.setBackground(getResources().getDrawable(R.drawable.titlebar_blue_cube));
        return titleBar;
    }

    @Override
    public void onClick(View view) {

    }

    @NonNull
    @Override
    protected FragmentReportViewBinding viewBindingInflate(LayoutInflater inflater, ViewGroup container) {
        return FragmentReportViewBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
        chart = findViewById(R.id.credit_chart);

        initChartStyle();
        initChartLabel();
        setChartData(5, 80);

        chart.animateXY(1400, 1400, Easing.EaseInOutQuad);
    }

    /**
     * 初始化图表样式
     */
    protected void initChartStyle() {
        chart.setRotationEnabled(false);

        chart.getDescription().setEnabled(false);
        chart.setWebColorInner(Color.rgb(117, 74, 187));
        chart.setWebLineWidthInner(1f);
        chart.setWebColor(Color.rgb(117, 74, 187));
        chart.setWebLineWidth(1f);
        chart.setWebAlpha(100);
        chart.setBackgroundColor(Color.argb(0, 1, 1, 1));

        MarkerView mv = new RadarMarkerView(getContext(), R.layout.marker_view_radar);
        mv.setChartView(chart);
        chart.setMarker(mv);

        initXYAxisStyle();
    }

    /**
     * 设置XY轴样式
     */
    private void initXYAxisStyle() {
        //设置X轴（雷达图项目点）的样式
        XAxis xAxis = chart.getXAxis();
        xAxis.setTextSize(9f);
        xAxis.setYOffset(0f);
        xAxis.setXOffset(0f);
        xAxis.setValueFormatter(new ValueFormatter() {
            private final String[] mActivities = new String[]{"央行征信", "芝麻信用", "微信支付分", "鹏元征信", "京东小白守约"};

            @Override
            public String getFormattedValue(float value) {
                return mActivities[(int) value % mActivities.length];
            }
        });
        xAxis.setTextColor(Color.BLACK);

        //设置Y轴（雷达图的分值）的样式
        YAxis yAxis = chart.getYAxis();
        yAxis.setLabelCount(5, false);
        yAxis.setTextSize(9f);

        yAxis.setAxisMinimum(0f);
        yAxis.setAxisMaximum(70f);

        yAxis.setDrawLabels(false);
    }

    /**
     * 初始化图表标题
     */
    protected void initChartLabel() {
        chart.getLegend().setEnabled(false);
//        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
//        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
//        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
//        l.setDrawInside(false);
//        l.setXEntrySpace(7f);
//        l.setYEntrySpace(5f);
//        l.setTextColor(Color.WHITE);
    }

    /**
     * 设置图表数据
     *
     * @param count
     * @param range
     */
    protected void setChartData(int count, float range) {
        float min = 20;

        ArrayList<RadarEntry> entries = new ArrayList<>();

//        for (int i = 0; i < count; i++) {
//            float val = (float) (Math.random() * range) + min;
//            entries.add(new RadarEntry(val));
//        }

        entries.add(new RadarEntry(90));
        entries.add(new RadarEntry(80));
        entries.add(new RadarEntry(87));
        entries.add(new RadarEntry(92));
        entries.add(new RadarEntry(87));


        RadarDataSet set = new RadarDataSet(entries, "信用评级");
        set.setColor(Color.rgb(157, 195, 230));
        set.setFillColor(Color.rgb(157, 195, 230));
        set.setDrawFilled(true);
        set.setFillAlpha(180);
        set.setLineWidth(2f);
        set.setDrawHighlightCircleEnabled(true);
        set.setDrawHighlightIndicators(false);

        ArrayList<IRadarDataSet> sets = new ArrayList<>();
        sets.add(set);

        RadarData data = new RadarData(sets);
        data.setValueTextSize(8f);
        data.setDrawValues(false);
        data.setValueTextColor(Color.WHITE);

        chart.setData(data);
        chart.invalidate();
    }
}
